<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: yangweijie <yangweijiester@gmail.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Admin\Controller;
use Admin\Model\AuthGroupModel;

/**
 * 订单管理
 * @author yuanshao
 */
class OrderController extends AdminController {

	public function _filter(&$map)
	{
		$status=I('status');
		if($status!='')$map['status']=$status;
	}

	public function _before_index()
	{
        //获取左边菜单
        // $this->getMenu();
		$statusarr[0]='待付款';
		$statusarr[1]='待发货';
		$statusarr[2]='已发货';
		$statusarr[3]='已完成';
		$statusarr[4]='已取消';
		$this->assign('statusarr',$statusarr);
		$typearr[0]='送货上门';
		$typearr[1]='门店自提';
		$this->assign('typearr',$typearr);
        $userids = M('user')->getField('id',true);
        $map['user_id'] = array('in',$userids);
        $map['is_del'] = 0;
		$statusnum['total']=M('Order')->where($map)->count();
        $map['is_drawback'] = 0;
		$list	=	M('Order')->where($map)->select();
		$statusnum[0]=0;
		$statusnum[1]=0;
		$statusnum[2]=0;
		$statusnum[3]=0;
		$statusnum[4]=0;
		//$statusnum['total']=count($list);
		foreach($list as $item)
		{
			$statusnum[$item['status']]++;
		}
		$this->assign('statusnum',$statusnum);
	}

	//
	 public function index() {
		$model_name	=	$this->model_name?$this->model_name:CONTROLLER_NAME;
        $model = D($model_name);
       
        $status = I('status');
        $isdraw = I('isdraw');
       	$map = array();
       	$map['is_del'] = 0;
        $id = I('title');
        $excel_url = U('orders_excel');
        if (!empty($id)) {
        	$map['sn'] = array('like',"%$id%");
        	$excel_url .= "&sn=".$id;
        }
        
        $stat = I('time_start');
        $end = I('time_end');
        if ($stat){
        	$start = strtotime($stat);
        	$map['addtime'] = array('egt',$start);
        	$excel_url .="&start=$start";
        }
        if ($end){
        	$endt = strtotime($end." 23:59:59");
        	$map['addtime'] = array('elt',$endt);
        	$excel_url.="&endtime=$endt";
        }
        if ($stat&&$end){
        	$start = strtotime($stat);
        	$endt = strtotime($end." 23:59:59");
        	$map['addtime'] = array('between',$start.",".$endt);
        }
        if ($status!="") {
        	$map['status'] = (int)$status;
        	$excel_url.="&status=$status";
        }
        $userids = M('user')->getField('id',true);
        $mobile = I('mobile');
        if ($mobile!=""){
        	$userids = M('user')->where('mobile like "%'.$mobile.'%"')->getField('id',true);
        }
        $userid = I('user_id');
        if($userid!="") $userids = $userid;
        $map['user_id'] = array('in',$userids);
        $where['user_id'] = array('in',$userids);
        $excel_url.="&userid=".implode(',', $userids);
        
        if($status!=""&&$isdraw=="") {
        	$map['is_drawback'] = 0;
        	$excel_url.="&drawback=0";
        }
       
        if ($isdraw!="") {
        	$map['is_drawback'] = $isdraw;
        	$excel_url.="&drawback=".$isdraw;
        }
        $count = M('order')->where($map)->count();
        
        $p = new \Think\Page($count, 20);
        $list = M('order')->limit($p->firstRow.','.$p->listRows)->where($map)->order('id DESC')->select();
        foreach ($list as $k=>$v){
        	$list[$k]['name'] = M('user')->where('id='.$v['user_id'])->getField('name');
        }
        
        //已退款订单数量
        $where['is_drawback'] = 1;
        $where['is_del'] = 0;
        $drawbacknum = M('order')->where($where)->count();
        $this->assign('drawbacknum',$drawbacknum);
        
        $page = $p->show ();
        $this->assign ( 'list', $list );
        $this->assign ( "page", $page );
        $this->assign('excel_url',$excel_url);
        
		$this->display();
    }


	//改变状态
	public function upstatus()
	{
		$order_id	=	intval(I('id'));
		$data['status']=intval(I('status'));
		if ($data['status']==2){
	        $data['sendtime'] = time();
        }
		$map['id']	=	$order_id;
		M('Order')->where($map)->save($data);
		if ($data['status']=='3') {
			# code...
			$this->enddeal($order_id);
		}
		
		$this->success('改变成功');
	}
	public function views()
	{  
        //获取左边菜单
        // $this->getMenu();
		$order_id=	intval(I('id'));
		$order=M('Order')->where('id='.$order_id)->find();
		$this->assign('order',$order);
		$list=M('OrderDetail')->where('order_id='.$order_id)->select();
		foreach($list as $key=>$item)
		{
			//$list[$key]['product'] = M('Product')->where('id='.$item['product_id'])->find();
			$product = M('Product')->where('id='.$item['product_id'])->find();
			$list[$key]['title'] = $product['title'];
			
		}
		$this->assign('list',$list);
// 		print_r($list);die;
		$typearr[0]='送货上门';
		$typearr[1]='门店自提';
		$this->assign('typearr',$typearr);
		//送货地址
		$address	=	M('UserAddress')->where('id='.$order['address_id'])->find();
		$this->assign('address',$address);
		/*if($address)
		{
			$area=M('Area')->where('id='.$address['area_id'])->find();
			$this->assign('area',$area);
		}*/
		//自提点
		$store	=	M('store')->where('id='.$order['depot_id'])->find();
		$this->assign('store',$store);
		
		//查看订单所得积分
		$jifen = M('integral_record')->where('type=3 and orderid='.$order_id)->getField('integral');
		$this->assign('integral',$jifen);
		//
		$invoicearr[0]='否';
		$invoicearr[1]='是';
		$this->assign('invoicearr',$invoicearr);
		$this->display();
	}


	//交易完成
	public function enddeal($order_id ="1"){

		$grade =M('grade');
		$user =M('user');
		$info =M('Order')->where('id ='.$order_id)->find();
		$userid =$info['user_id'];
		$ordercost =floatval($info['money']);

		$userpoints =$user->where('id='.$userid)->getField('points');
		$gradeinfo =$grade->order('id ASC')->select();
		$userpoints =floatval($userpoints)+$ordercost;
		$gradeArr =$grade->order('glv ASC')->select();
		foreach ($gradeArr as $key => $value) {
			# code...
			if ($userpoints>$gradeArr[$key]['glimit']) {
				# code...
				M('user')->where('id='.$userid)->setField('glv',$gradeArr[$key]['glv']);
			}
		}
		$rel =$user->where('id='.$userid)->setField('points',$userpoints);
	}
	
	/**
	 * 导出订单
	 */
	public function orders_excel(){
		$m=M('order');
		$map = array();
		$map['is_del'] = 0;
		$status = I('status');
		if($status!=""){
			$map['status'] = $status;
		}
		$title = I('sn');
		if ($title!=""){
			$map['sn'] = array('like',"%$title%");
		}
		$draw = I('drawback');
		if($draw!=""){
			$map['is_drawback'] = $draw;
		}
	
		$ids    =  rtrim(I('ids'),',');
		if (!empty($ids)){
			$map['id'] = array('in',$ids);
		}
		$userid = I('userid');
		if($userid!=""){
			$map['user_id'] = array('in',$userid);
		}
	
		//$join = "onethink_user_address as adr on adr.id=o.address_id";
		//$field = "o.*,adr.name,adr.mobile,adr.area,adr.address";
		$list = $m->where($map)->order("id DESC")->select();
		// 		print_r($m->getLastsql());die;
		$str = '
		<table border="1">
		<tr style="background-color:#ccffcc;min-height:30px;">
		<th align="center">订单编号</th>
		<th align="center">下单时间</th>
		<th align="center">订单状态</th>
		<th align="center">订单金额</th>
		<th align="center">支付方式</th>
		<th align="center">运费</th>
		<th align="center">取货方式</th>
		<th align="center">收货人</th>
		<th align="center">收货电话</th>
		<th align="center">收货地址</th>
		<th align="center">门店名称</th>
		<th align="center">门店地址</th>
		<th align="center">联系电话</th>
		<th align="center">商品名称</th>
		<th align="center">商品单价</th>
		<th align="center">购买数量</th>
		</tr>
		';
		foreach ($list as $k => $v) {
			//查找订单商品信息
			$pros = M('order_detail')->alias("d")->join("LEFT JOIN onethink_product as p on p.id=d.product_id")->where("d.order_id='".$v['id']."'")->order("d.id ASC")->field("d.*,p.title")->select();
			$num = count($pros);
			if (($k%2)==0){
				$str .='<tr align="center" style="background-color:#fdffcc;min-height:30px;">';
			}else{
				$str .='<tr align="center" style="min-height:30px;">';
			}
			$str .= '
			<td rowspan='.$num.'>'.$v['sn'].'&nbsp;</td>
			<td rowspan='.$num.'>'.date('Y-m-d H:i',$v['addtime']).'</td>';
			if($v['status']==0){
				$str.='<td rowspan='.$num.'>待付款</td>';
			}elseif($v['status']==1){
				if($v['type']==1){
					$str.='<td rowspan='.$num.'>待自提</td>';
				}else{
					$str.='<td rowspan='.$num.'>待发货</td>';
				}
			}elseif($v['status']==2){
				$str.='<td rowspan='.$num.'>待收货</td>';
			}elseif($v['status']==3){
				$str.='<td rowspan='.$num.'>交易完成</td>';
			}elseif ($v['status']==4){
				$str.='<td rowspan='.$num.'>交易取消</td>';
			}
			
			$str .='<td rowspan='.$num.'>'.$v['money'].'&nbsp;</td>';
			if($v['type']==1){
				$str.='<td rowspan='.$num.'>余额支付</td>';
			}else{
				$str.='<td rowspan='.$num.'>微信支付</td>';
			}
			$str.='<td rowspan='.$num.'>'.$v['express_money'].'&nbsp;</td>';
			if($v['type']==1){
				$store = M('store')->where('id='.$v['depot_id'])->find();
				$str.='
				<td rowspan='.$num.'>门店自提</td>
				<td rowspan='.$num.'>&nbsp;</td>
				<td rowspan='.$num.'>&nbsp;</td>
				<td rowspan='.$num.'>&nbsp;</td>
				<td rowspan='.$num.'>'.$store['sname'].'</td>
				<td rowspan='.$num.'>'.$store['sadd'].'</td>
				<td rowspan='.$num.'>'.$store['mobile'].'&nbsp;</td>';
			}else{
				$str.='
				<td rowspan='.$num.'>送货上门</td>
				<td rowspan='.$num.'>'.$v['name'].'</td>
				<td rowspan='.$num.'>'.$v['mobile'].'&nbsp;</td>
				<td rowspan='.$num.'>'.$v['area'].$v['address'].'</td>
				<td rowspan='.$num.'>&nbsp;</td>
				<td rowspan='.$num.'>&nbsp;</td>
				<td rowspan='.$num.'>&nbsp;</td>';
			}
			
				
			foreach($pros as $key=>$val){
	
				if ($num>1){
					if ($key==0){
						$str .= '
						<td>'.$val['title'].'</td>
						<td>'.$val['product_price'].'&nbsp;</td>
						<td>'.$val['product_num'].'&nbsp;</td></tr>';
					}elseif ($key>0){
						if (($k%2)==0){
							$str .='<tr align="center" style="background-color:#fdffcc;min-height:30px;">';
						}else{
							$str .='<tr align="center" style="min-height:30px;">';
						}
	
						$str .= '
						<td>'.$val['title'].'</td>
						<td>'.$val['product_price'].'&nbsp;</td>
						<td>'.$val['product_num'].'&nbsp;</td></tr>
						';
					}
				}else {
					$str .= '
					<td>'.$val['title'].'</td>
					<td>'.$val['product_price'].'&nbsp;</td>
					<td>'.$val['product_num'].'&nbsp;</td></tr>';
				}
	
			}
			//$str.='</table></td></tr>';
	
		}
		$str .="</table>";
// 		print_r($str);die;
		//输出
		header('Content-Length: '.strlen($str));
		header("Content-type:application/vnd.ms-excel;charset=UTF-8");
		header("Content-Disposition:filename=orders.xls");
		echo $str;
	}


	public function detail(){

       $order_id =  I("get.order_id");
       if($order_id){
          $data = D("order")->find($order_id);

       }
    }
}
